package com.seashell;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        if(args.length == 0){
            System.out.println("command format: java -jar xxx.jar <filepath> [pageSize]");
            return;
        }
        String arg = args[1];
        Integer pageSize = 10000;
        if(arg!=null && arg.trim()!=""){
            pageSize = Integer.parseInt(arg);
        }

        try {
            ExcelReader reader = new ExcelReader(args[0]);
            List<Map<String, Object>> maps = reader.doRead();
            Integer totalPage = maps.size()/pageSize;
            Integer overRowsCount =  maps.size() % pageSize;
            if(overRowsCount!=0){
                totalPage++;
            }
            for (int pageCount = 0; pageCount < totalPage; pageCount++) {
                FileWriter writer = new FileWriter("generate_sql",pageCount);
                int firstRow = pageCount * pageSize;
                int lastRow = (pageCount==totalPage-1) ? maps.size() : (pageCount+1)*pageSize;

                for (int i = firstRow; i < lastRow; i++) {

                    Map<String, Object> rowMap = maps.get(i);

                    String make = SqlCreator.make(rowMap.get("SHEET"), rowMap.get("EGRADE"),
                            rowMap.get("EMPNO"), rowMap.get("ENAME"), rowMap.get("JOB"), rowMap.get("SAL"));
                    writer.write(make);
                }
                writer.completeWrite();
            }


        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
